<template>
  <div class="app-container" style="margin: 0;padding-left: 0;padding-right: 0;">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="管理企业" prop="companyAdminId">
            <el-select v-model="queryParams.companyAdminId" value-key="companyId" placeholder="请选择" clearable filterable size="medium" @change="change">
              <el-option
                v-for="dict in enterpriseDropDownOptions"
                :key="dict.companyId"
                :label="dict.companyName"
                :value="dict.companyId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="客户企业" prop="customerCompanyId" v-if="isShow">
            <el-select v-model="queryParams.customerCompanyId" placeholder="请选择" clearable size="medium" filterable>
              <el-option
                v-for="dict in clientCompany"
                :key="dict.companyId"
                :label="dict.companyName"
                :value="dict.companyId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="申请状态" prop="registerStatus">
            <el-select
              v-model="queryParams.registerStatus"
              placeholder="全部"
              clearable
              size="medium"
            >
              <el-option label="未注册" value="0"></el-option>
              <el-option label="进行中" value="1"></el-option>
              <el-option label="已注册" value="2"></el-option>
              <el-option label="注销中" value="3"></el-option>
              <el-option label="已注销" value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否有税号" prop="hasTaxNo" label-width="90px">
            <el-select
              v-model="queryParams.hasTaxNo"
              placeholder="请选择"
              clearable
              size="medium"
            >
              <el-option label="全部" value="A"></el-option>
              <el-option label="有" value="Y"></el-option>
              <el-option label="没有" value="N"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="注册时间" prop="registerTimeStart" label-width="100px">
              <el-date-picker
              v-model="queryParams.registerTimeStart"
              size="medium"
              style="width: 140px"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="开始日期">
              </el-date-picker>
          </el-form-item>
          <el-form-item>-</el-form-item>
          <el-form-item prop="registerTimeEnd">
              <el-date-picker
              v-model="queryParams.registerTimeEnd"
              size="medium"
              style="width: 140px"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="结束日期">
              </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
          </el-form-item>
          <el-form-item>
            <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="medium"
            @click="handleBatchExport"
            v-hasPermi="['taxmsg:taxpayer:selfEmployedListDownload']"
          >导出数据</el-button>
          </el-form-item>
        </el-form>

        <!-- <el-row :gutter="10" class="mb10">
          <el-col :span="1.5">
           
          </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> 
        </el-row> -->

        <el-table v-loading="loading" :data="selfEmployedList">
          
          <el-table-column fixed label="序号" prop="" width="55" align="center">
            <template slot-scope="scope">
              <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column label="个体户名称" align="left" key="enterpriseName" prop="enterpriseName" width="350"/>
          <el-table-column label="统一社会信用代码" align="left" key="taxNo" prop="taxNo" width="200"/>
          <el-table-column label="注册地" align="left" key="provinceFullName" prop="provinceFullName" />
          <el-table-column label="申请人" align="left" key="name" prop="name"/>
          <el-table-column label="证件号" align="left" key="idNumber" prop="idNumber" width="150"/>
          <el-table-column label="手机号" align="left" key="phone" prop="phone" width="130"/>
          <el-table-column label="申请/注销日期" align="left" key="registerDate" prop="registerDate" width="180"/>
          <el-table-column label="申请状态" align="left" prop="registerStatus" :formatter="registerFormat"/>
          <el-table-column label="管理企业" align="left" key="companyAdminName" prop="companyAdminName" width="250"/>
          <el-table-column label="客户企业" align="left" key="customerCompanyName" prop="customerCompanyName" width="250"/>
          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="150">
            <template slot-scope="scope">
              <el-dropdown size="mini" split-button type="primary" @click="handleView(scope.row)" placement="bottom-start" trigger="click">
                详 情
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item icon="el-icon-edit" @click.native="handleEdit(scope.row)" v-hasPermi="['taxmsg:taxpayer:updateRegisterStatus']">编辑状态</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
            >详情</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 编辑状态对话框 -->
    <el-dialog :title="title" :visible.sync="openSelfEmployedUpdate" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="申请状态" prop="registerStatus">
          <el-select
            v-model="form.registerStatus"
            placeholder="全部"
            clearable
            size="small"
          >
            <el-option label="未注册" value="0"></el-option>
            <el-option label="进行中" value="1"></el-option>
            <el-option label="已注册" value="2"></el-option>
            <el-option label="注销中" value="3"></el-option>
            <el-option label="已注销" value="4"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelSelfEmployedUpdate">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 纳税人信息详情 -->
    <el-dialog title="纳税人信息详情" :visible.sync="openView" width="900px" append-to-body>
      <el-form ref="form" :model="form" label-width="150px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="真实姓名：">{{ form.realName }}</el-form-item>
            <el-form-item label="性别：">{{ userGenderFormat(form) }}</el-form-item>
            <el-form-item label="身份证号：">{{ form.idNumber }}</el-form-item>
            <el-form-item label="手机号：">{{ form.phone }}</el-form-item>
            <el-form-item label="纳税人类型：">{{ typeFormat(form) }}</el-form-item>
            <el-form-item label="申请方式：">{{ form.registerWay }}</el-form-item>
            <el-form-item label="申请时间：">{{ form.registerTime }}</el-form-item>
            <el-form-item label="管理企业：">{{ form.companyAdminName }}</el-form-item>
            <el-form-item label="客户企业：">{{ form.customerCompanyName }}</el-form-item>
            <el-form-item label="客户企业代发：">{{ customerSendFlagFormat(form) }}</el-form-item>
            <el-form-item label="客户企业产品渠道：">{{ customerChannelsFormat(form) }}</el-form-item>
            <el-form-item label="客户企业是否结算：">{{ customerSettleFlagFormat(form) }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在省份：">{{ form.provinceFullName }}</el-form-item>
            <el-form-item label="纳税人：">{{ form.name }}</el-form-item>
            <el-form-item label="个体户名称：">{{ form.enterpriseName }}</el-form-item>
            <el-form-item label="税号：">{{ form.taxNo }}</el-form-item>
            <el-form-item label="申请状态：">{{ registerIntFormat(form) }}</el-form-item>
            <el-form-item label="注销时间：">{{ form.closeTime }}</el-form-item>
            <el-form-item label="个税申报密码：">{{ form.applyTaxPwd }}</el-form-item>
            <el-form-item label="税局登录密码：">{{ form.loginPwd }}</el-form-item>
            <el-form-item label="状态：">{{ statusFormat(form) }}</el-form-item>
            <el-form-item label="创建时间：">{{ form.createTime }}</el-form-item>
            <el-form-item label="修改时间：">{{ form.updateTime }}</el-form-item>
          </el-col>
          
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selfEmployedList, selfEmployedListDownload, registerStatusUpdate, taxPersonListDetail } from "@/api/taxmsg/taxpayer";
export default {
  props: {
    payer: {
      type: Object
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 用户表格数据
      selfEmployedList: null,
       // 所有的管理企业和关联客户企业信息数据字典
      enterpriseDropDownOptions: [],
      clientCompany:[],
      // 是否显示弹出层（编辑状态）
      openSelfEmployedUpdate: false,
      // 是否显示弹出层
      form: {},
      open: false,
      // 是否显示详细弹出层
      openView: false,
      isShow:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        companyAdminId: undefined,
        customerCompanyId: undefined,
        registerStatus: undefined,
        hasTaxNo: undefined,
        registerTimeStart: undefined,
        registerTimeEnd: undefined
      },
      // 编辑状态表单校验
      rules: {
        registerStatus: [
          { required: true, message: "请选择申请状态", trigger: "blur" }
        ]
      }
     
    };
  },
  created() {
    this.getList();
    this.getEnterpriseDropDown().then(response => {
      this.enterpriseDropDownOptions = response.data;
    });
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      selfEmployedList(this.queryParams).then(response => {
          this.selfEmployedList = response.data.tableData;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    change(e){
      delete this.queryParams.companyId
      if(e){
        this.isShow = true;
        this.clientCompany = this.enterpriseDropDownOptions.filter(item => item.companyId === e)[0].clientCompany;
      }else{
        this.isShow = false
      }
    },
    // 性别状态翻译
    userGenderFormat(row) {
      let userGender = row.userGender;
      switch (userGender) {
        case '0':
        userGender = "未知";
          break;
        case '1':
        userGender = "男";
          break;
        case '2':
        userGender = "女";
          break;
      }
      return userGender;
    },
    // 纳税人类型翻译
    typeFormat(row) {
      let type = row.type;
      switch (type) {
        case 0:
        type = "临时税务登记";
          break;
        case 1:
        type = "个体工商户";
          break;
      }
      return type;
    },
    // 客户企业代发翻译
    customerSendFlagFormat(row) {
      let sendFlag = row.customerSendFlag;
      switch (sendFlag) {
        case '0':
        sendFlag = "否";
          break;
        case '1':
        sendFlag = "代发";
          break;
      }
      return sendFlag;
    },
    // 客户企业代发翻译
    customerChannelsFormat(row) {
      let channels = row.customerChannels;
      switch (channels) {
        case '1':
        channels = "单笔";
          break;
        case '2':
        channels = "批量";
          break;
        case '3':
        channels = "单笔批量";
          break;
      }
      return channels;
    },
    // 是否结算翻译
    customerSettleFlagFormat(row) {
      let settleFlag = row.customerSettleFlag;
      switch (settleFlag) {
        case '0':
        settleFlag = "否";
          break;
        case '1':
        settleFlag = "结算";
          break;
      }
      return settleFlag;
    },
    // 状态翻译
    statusFormat(row) {
      let status = row.status;
      switch (status) {
        case 0:
        status = "无效";
          break;
        case 1:
        status = "有效";
          break;
      }
      return status;
    },
    // Int申请状态翻译
    registerIntFormat(row) {
      let status = row.registerStatus;
      switch (status) {
        case 0:
        status = "未注册";
          break;
        case 1:
        status = "进行中";
          break;
        case 2:
        status = "已注册";
          break;
        case 3:
        status = "注销中";
          break;
        case 4:
        status = "已注销";
          break;  
      }
      return status;
    },
    // 申请状态翻译
    registerFormat(row) {
      let status = row.registerStatus;
      switch (status) {
        case '0':
        status = "未注册";
          break;
        case '1':
        status = "进行中";
          break;
        case '2':
        status = "已注册";
          break;
        case '3':
        status = "注销中";
          break;
        case '4':
        status = "已注销";
          break;  
      }
      return status;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
      this.isShow = false;
    },
    cancelSelfEmployedUpdate() {
      this.openSelfEmployedUpdate = false;
      this.reset();
    },
    reset() {
      this.form = {
        registerStatus: undefined
      };
      this.resetForm("form");
    },
    /** 编辑状态按钮操作 */
    handleEdit(row) {
      this.reset();
      this.form.ids= [row.id];

      if(row.registerStatus=="0"){
        this.form.registerStatus = "未注册";
      }else if(row.registerStatus=="1"){
        this.form.registerStatus = "进行中";
      }else if(row.registerStatus=="2"){
        this.form.registerStatus = "已注册";
      }else if(row.registerStatus=="3"){
        this.form.registerStatus = "注销中";
      }else if(row.registerStatus=="4"){
        this.form.registerStatus = "已注销";
      }
      this.openSelfEmployedUpdate = true;
      this.title = "编辑";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.registerStatus == "未注册"){
            this.form.registerStatus = "0"
          }else if(this.form.registerStatus == "进行中"){
            this.form.registerStatus = "1"
          }else if(this.form.registerStatus == "已注册"){
            this.form.registerStatus = "2"
          }else if(this.form.registerStatus == "注销中"){
            this.form.registerStatus = "3"
          }else if(this.form.registerStatus == "已注销"){
            this.form.registerStatus = "4"
          }
          registerStatusUpdate(this.form).then(response => {
              this.msgSuccess(response.msg);
              this.openSelfEmployedUpdate = false;
              this.getList();
            });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 导出按钮操作 */
    handleBatchExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return selfEmployedListDownload(queryParams);
        }).then(response => {
          //this.download(response.msg);
          var list_name = "个体工商户";
          this.export_excel_file(response, list_name);
        }).catch(()=>{})
    },
    /** 纳税人信息详情 */
    handleView(row) {
      taxPersonListDetail(row.id).then(response => {
        this.form = response.data;
        this.openView = true;
      });
    },
  }
};
</script>
